/**
 * 
 */
package com.arethusa;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;

import java.awt.Dimension;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer;

import simpleorm.core.*;
import simpleorm.ui.*;

import com.arethusa.langebaandb.*;
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Index;
import com.healthmarketscience.jackcess.Table;
import com.ibm.j2x.swing.util.TableUtilities;
import com.ibm.j2x.util.CollectionUtilities;
import com.ibm.xslt4j.bcel.classfile.Attribute;

import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.WindowConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.awt.SystemColor;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.awt.GridBagLayout;

/**
 * @author tpijl
 *
 */
public class InvoerenUitslag extends JPanel implements SUIConstants {

	static final long serialVersionUID=1;
	private InvoerenUitslag parent = this;  //  @jve:decl-index=0:visual-constraint="473,10"
	private int ProgrammaNr;
	private JPanel prvwPanel = null;
	private List<StartBean> starts;  //  @jve:decl-index=0:
	private List<StartBean> mstarts = new ArrayList<StartBean>();  //  @jve:decl-index=0:
	private StartBean start;
	private Properties props = new Properties();  //  @jve:decl-index=0:

	private JPanel enrtyPanel = null;
	private JPanel programmaPanel = null;
	private JLabel jLabel = null;
	private JComboBox programmaComboBox = null;
	private JPanel invoerPanel = null;
	private JPanel capPanel = null;
	private JLabel jLabel1 = null;
	private JTextField capNrTextField = null;
	private JPanel eindTijdPanel = null;
	private JLabel jLabel2 = null;
	private JTextField eindTijdTextField = null;
	private JPanel disPanel = null;
	private JLabel jLabel3 = null;
	private JTextField disTextField = null;
	private JPanel buttonPanel = null;
	private JButton printButton = null;
	private JButton voegToeButton = null;
	private JScrollPane tableScrollPane = null;
	private JTable table = null;
	private JButton SplashImportButton = null;
	private JDialog selectDialog = null;  //  @jve:decl-index=0:visual-constraint="15,518"
	private JPanel selectDialogContentPane = null;  //  @jve:decl-index=0:visual-constraint="312,523"
	private JScrollPane selectDialogScrollPane = null;
	private JButton selectButton = null;
	private JList capsList = null;
	private JTextArea selectionText = null;
	private DefaultListModel listModel = new DefaultListModel();
	private JPanel extraButtonPanel = null;
	
	private class ProgComboActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
	        try {
				JComboBox cb = (JComboBox)arg0.getSource();
				String pnr = ((String)cb.getSelectedItem()).substring(0,2);
				ProgrammaNr = (pnr.equalsIgnoreCase("<K")?0:new Integer(pnr).intValue());
				fillTable();
				if(ProgrammaNr != 0 ) {
					parent.capNrTextField.requestFocusInWindow();
					getSplashImportButton().setEnabled(true);
				} else {
					getSplashImportButton().setEnabled(false);
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
	}
	
	private class EntryButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent ae) {
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			try {
				Integer capnr = new Integer(getCapNrTextField().getText());
				SPreparedStatement stmt = Start.meta.select(   
						"PROGNR="+ new Integer(ProgrammaNr).toString()+" AND CapNr="+capnr.toString(),"CAPNR");
				SResultSet res = stmt.execute();
				// haal alle starts op
				mstarts.clear();
				while( res.hasNext() ) {
					StartBean strt = new StartBean((Start)res.getRecord());
			        mstarts.add(strt);
				}
				if( ! hasBegun ) { SConnection.commit(); }
				int nr = mstarts.size();
				if( nr == 0 ) {
					// Niet gevonden
					JOptionPane.showMessageDialog(null,
							"Capnr "+getCapNrTextField().getText()+" bestaat niet.",
							"Validatie", JOptionPane.ERROR_MESSAGE);
				} else {
					if( nr > 1 ) {
						// Er zijn meerdere zwemmers met het opgegeven capnummer.
						getSelectionText().setText("Er zijn meerdere\nzwemmers met cap "+
								capnr.toString());
						listModel.clear();
						for (StartBean strt : mstarts) {
							String sel = strt.getCapnr()+" "+strt.getCapkleur();
					        listModel.addElement(sel);
						}
				        getCapsList().setModel(listModel);
						getSelectDialog().setVisible(true);
					} else {
						start = mstarts.get(0);
					}
					String nwTijd = CheckTijd(getEindTijdTextField().getText());
					//Als er al een eindtijd ingevuld is, vraag of overschreven mag worden
					if( (start.getEindtijd() != null && start.getEindtijd().length() != 0 ) 
							|| (start.getDis() != null && start.getDis().length() != 0) ) {
						if( JOptionPane.showConfirmDialog(null, 
								"Capnr is al ingevuld. Overschrijven?", "Waarschuwing", 
								JOptionPane.YES_NO_OPTION, 
								JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION ) {
							//Overschrijven
							start.setEindtijd(nwTijd);
							start.setDis(getDisTextField().getText());
							start.update();
							fillTable();
						}
					} else {
						//Invullen
						start.setEindtijd(nwTijd);
						start.setDis(getDisTextField().getText());
						start.update();
						fillTable();
					}
					parent.capNrTextField.setText("");
					parent.capNrTextField.requestFocusInWindow();
					parent.eindTijdTextField.setText("");
					parent.disTextField.setText("");
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null,
						"Capnr "+getCapNrTextField().getText()+" is geen nummer.",
						"Validatie", JOptionPane.ERROR_MESSAGE);
				if( ! hasBegun ) { SConnection.commit(); }
			} catch (TijdException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage(),
						"Validatie", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	private class PrintButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent ae) {
			// Bepaal eerst de statistieken : #starts, #na, #ng, #ziek, #prijzen
			Helper.BepaalStatistieken();
			// en print vervolgens
			PrintPreviewDialog prvw = new PrintPreviewDialog();
			Map<String, Integer> parms = new HashMap<String, Integer>();
			parms.put("ProgNr", new Integer(ProgrammaNr));
			
			JasperPrint prt = new JasperPrint();
			try {
				boolean hasBegun = SConnection.getConnection().hasBegun();
				if( ! hasBegun ) { SConnection.begin(); }
				prt = JasperFillManager.fillReport("Reports\\Uitslag.jasper", parms, SConnection.getBegunDBConnection());
				SConnection.commit();
			} catch (JRException re) {
				JOptionPane.showMessageDialog(null,
						re.getMessage()+re.getCause(),
						"Report foutmelding", JOptionPane.ERROR_MESSAGE);
			}
			if( prvwPanel != null) prvw.getContentPane().remove(prvwPanel);
			prvwPanel = new JRViewer(prt);
			prvw.getContentPane().add(prvwPanel, java.awt.BorderLayout.CENTER);
			prvw.setSize(800,950);
			prvw.setVisible(true);
		}
		
	}
	
	private class SplashImportButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			if( ProgrammaNr > 0 ) {
				try {
					Database mdb = Database.open(new File(props.getProperty("mdbfile")));
					Table dsqitem = mdb.getTable("DSQITEM");
					List<String> dsqColumnNames = new ArrayList<String>();
					dsqColumnNames.add("DSQITEMID");
					dsqColumnNames.add("CODE");
					Map<Integer,String> discodes = new HashMap<Integer,String>();
					Map<String, Object> row = null;
					while( (row = dsqitem.getNextRow(dsqColumnNames)) != null){
						discodes.put((Integer)row.get("DSQITEMID"), (String)row.get("CODE"));
					}
					Table athlete = mdb.getTable("ATHLETE");
					List<String> athleteColumnNames = new ArrayList<String>();
					athleteColumnNames.add("ATHLETEID");
					athleteColumnNames.add("LICENSE");
					Map<Integer,String> startnrs = new HashMap<Integer,String>();
					row = null;
					while( (row = athlete.getNextRow(athleteColumnNames)) != null){
						startnrs.put((Integer)row.get("ATHLETEID"), (String)row.get("LICENSE"));
					}
					Table event = mdb.getTable("SWIMEVENT");
					List<String> eventColumnNames = new ArrayList<String>();
					eventColumnNames.add("SWIMEVENTID");
					eventColumnNames.add("EVENTNUMBER");
					Map<Short,Integer> prognrs = new HashMap<Short,Integer>();
					row = null;
					while( (row = event.getNextRow(eventColumnNames)) != null){
						prognrs.put((Short)row.get("EVENTNUMBER"), (Integer)row.get("SWIMEVENTID"));
					}
					Table swimresult = mdb.getTable("SWIMRESULT");
					Cursor resCursor = Cursor.createCursor(swimresult);
					List<String> resultColumnNames = new ArrayList<String>();
					resultColumnNames.add("ATHLETEID");
					resultColumnNames.add("SWIMEVENTID");
					resultColumnNames.add("DSQITEMID");
					resultColumnNames.add("SWIMTIME");
					while( resCursor.findNextRow(swimresult.getColumn("SWIMEVENTID"), new Integer(prognrs.get(new Short((short) ProgrammaNr))))) {
						row = resCursor.getCurrentRow(resultColumnNames);
						System.out.print("Prognr="+ProgrammaNr);	
						String startnr = startnrs.get(((Integer)row.get("ATHLETEID")));
						System.out.print(" Eindtijd="+row.get("SWIMTIME"));			
						System.out.println(" Dis="+row.get("DSQITEMID"));			
						boolean hasBegun = SConnection.getConnection().hasBegun();
						if( ! hasBegun ) { SConnection.begin(); }
						SPreparedStatement stmt = Start.meta.select(   
								"PROGNR="+ new Integer(ProgrammaNr).toString()+
								" AND STARTNR='"+startnr+"'"
								,"STARTNR");
						SResultSet res = stmt.execute();
						// haal alle starts op
						mstarts.clear();
						while( res.hasNext() ) {
							StartBean strt = new StartBean((Start)res.getRecord());
					        mstarts.add(strt);
						}
						if( ! hasBegun ) { SConnection.commit(); }
						int nr = mstarts.size();
						if( nr == 0 ) {
							// Niet gevonden
						} else {
							start = mstarts.get(0);
							//Invullen
							Integer tijd = ((Integer) row.get("SWIMTIME"))/10;
							String nwTijd = "";
							if(tijd > 0) {
								int hr = tijd/360000;
								int rest = tijd - hr * 360000;
								int mn = rest / 6000;
								int sc = rest - mn * 6000;
								nwTijd = String.format("%02d:%02d:%05.2f", hr,mn,sc/100.0);
								nwTijd = nwTijd.replace(',', '.');
							}
							start.setEindtijd(nwTijd);
							System.out.print("startnr="+startnr+" tijd="+nwTijd);
							String dis = "";
							Integer dsq = (Integer)row.get("DSQITEMID");
							if( dsq != null && dsq != 0 ) {
								if(nwTijd.length()==0) start.setEindtijd("00:00:00.00");
								dis = discodes.get(dsq);
								System.out.print(" dis="+dis);
							}
							start.setDis(dis);
							System.out.println();
							start.update();
						}
					}
					fillTable();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null,
						"Kies eerst een programma.",
						"Splash import", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	/**
	 * This is the default constructor
	 */
	public InvoerenUitslag() {
		super();
        //load a properties file
		try {
			props.load(new FileInputStream("splash.properties"));
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					"Kan het bestand 'splash.properties' niet vinden.",
					"Splash foutmelding", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage()+e.getCause(),
					"Splash foutmelding", JOptionPane.ERROR_MESSAGE);
		}
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(455, 471);
		this.add(getEnrtyPanel(), java.awt.BorderLayout.NORTH);
		this.add(getTableScrollPane(), java.awt.BorderLayout.CENTER);
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getEnrtyPanel() {
		if (enrtyPanel == null) {
			enrtyPanel = new JPanel();
			enrtyPanel.setLayout(new BorderLayout());
			enrtyPanel.add(getProgrammaPanel(), java.awt.BorderLayout.NORTH);
			enrtyPanel.add(getInvoerPanel(), java.awt.BorderLayout.WEST);
			enrtyPanel.add(getExtraButtonPanel(), BorderLayout.EAST);
		}
		return enrtyPanel;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getProgrammaPanel() {
		if (programmaPanel == null) {
			jLabel = new JLabel();
			jLabel.setText("Programma");
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			programmaPanel = new JPanel();
			programmaPanel.setLayout(flowLayout);
			programmaPanel.add(jLabel, null);
			programmaPanel.add(getProgrammaComboBox(), null);
		}
		return programmaPanel;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getProgrammaComboBox() {
		if (programmaComboBox == null) {
			programmaComboBox = new JComboBox();
			programmaComboBox.addActionListener(new ProgComboActionListener());
//			SConnection.begin();
			SPreparedStatement stmt = Programmanummers.meta.select(
					"1=1", "ProgNr");
			SResultSet res = stmt.execute();
			programmaComboBox .addItem("<Kies programma>");
			while (res.hasNext()) {
				Programmanummers prg = (Programmanummers)res.getRecord();
				int pnr = prg.get_fldPrognr().intValue();
				programmaComboBox .addItem(
						(pnr<10?"0":"")+pnr+" "+prg.get_fldTitel());
			}
//			SConnection.commit();
		}
		return programmaComboBox;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getInvoerPanel() {
		if (invoerPanel == null) {
			invoerPanel = new JPanel();
			invoerPanel.setLayout(new BoxLayout(getInvoerPanel(), BoxLayout.X_AXIS));
			invoerPanel.setPreferredSize(new Dimension(300, 50));
			invoerPanel.add(getCapPanel(), null);
			invoerPanel.add(getEindTijdPanel(), null);
			invoerPanel.add(getDisPanel(), null);
			invoerPanel.add(getButtonPanel(), null);
		}
		return invoerPanel;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getCapPanel() {
		if (capPanel == null) {
			jLabel1 = new JLabel();
			jLabel1.setPreferredSize(new Dimension(20, 20));
			jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
			jLabel1.setText("Cap#");
			jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			capPanel = new JPanel();
			capPanel.setLayout(new BorderLayout());
			capPanel.add(jLabel1, java.awt.BorderLayout.NORTH);
			capPanel.add(getCapNrTextField(), java.awt.BorderLayout.CENTER);
		}
		return capPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCapNrTextField() {
		if (capNrTextField == null) {
			capNrTextField = new JTextField();
			capNrTextField.setPreferredSize(new Dimension(40, 20));
		}
		return capNrTextField;
	}

	/**
	 * This method initializes jPanel5	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getEindTijdPanel() {
		if (eindTijdPanel == null) {
			jLabel2 = new JLabel();
			jLabel2.setPreferredSize(new Dimension(41, 20));
			jLabel2.setText("Eindtijd");
			jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
			eindTijdPanel = new JPanel();
			eindTijdPanel.setLayout(new BorderLayout());
			eindTijdPanel.add(jLabel2, java.awt.BorderLayout.NORTH);
			eindTijdPanel.add(getEindTijdTextField(), java.awt.BorderLayout.CENTER);
		}
		return eindTijdPanel;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getEindTijdTextField() {
		if (eindTijdTextField == null) {
			eindTijdTextField = new JTextField();
			eindTijdTextField.setComponentOrientation(ComponentOrientation.UNKNOWN);
			eindTijdTextField.setHorizontalAlignment(JTextField.LEADING);
			eindTijdTextField.setPreferredSize(new Dimension(160, 20));
		}
		return eindTijdTextField;
	}

	/**
	 * This method initializes jPanel6	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getDisPanel() {
		if (disPanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setPreferredSize(new Dimension(18, 20));
			jLabel3.setText("Dis");
			jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
			disPanel = new JPanel();
			disPanel.setLayout(new BorderLayout());
			disPanel.setPreferredSize(new Dimension(40, 36));
			disPanel.add(jLabel3, java.awt.BorderLayout.NORTH);
			disPanel.add(getDisTextField(), java.awt.BorderLayout.CENTER);
		}
		return disPanel;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDisTextField() {
		if (disTextField == null) {
			disTextField = new JTextField();
			disTextField.setPreferredSize(new Dimension(40, 20));
		}
		return disTextField;
	}

	/**
	 * This method initializes jPanel7	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new BorderLayout());
			buttonPanel.setPreferredSize(new Dimension(84, 50));
			buttonPanel.add(getPrintButton(), BorderLayout.NORTH);
			buttonPanel.add(getVoegToeButton(), java.awt.BorderLayout.CENTER);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPrintButton() {
		if (printButton == null) {
			printButton = new JButton();
			printButton.setPreferredSize(new Dimension(34, 25));
			printButton.setText("Print");
			printButton.setSelected(true);
			printButton.addActionListener(new PrintButtonActionListener());
		}
		return printButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getVoegToeButton() {
		if (voegToeButton == null) {
			voegToeButton = new JButton();
			voegToeButton.setText("Voeg toe");
			voegToeButton.addActionListener(new EntryButtonActionListener());
		}
		return voegToeButton;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getTableScrollPane() {
		if (tableScrollPane == null) {
			tableScrollPane = new JScrollPane();
			tableScrollPane.setViewportView(getTable());
		}
		return tableScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			table.setShowGrid(true);
		}
		return table;
	}

	@SuppressWarnings("unchecked")
	private void fillTable() {
		parent.setSize(new Dimension(455, 504));
		parent.setSize(new Dimension(524, 504));
		List<StartBean> tmp = new ArrayList<StartBean>();
		/// Prepare and execute the query
		if( ProgrammaNr > 0 ) {
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			SPreparedStatement stmt = Start.meta.select(
					"ProgNr="+ProgrammaNr, "CapNr");
			SResultSet res = stmt.execute();
			
			/// loop through the results
			while (res.hasNext()) {
				StartBean strt = new StartBean(((Start)res.getRecord()));
				tmp.add(strt);
			}
	    	SConnection.commit();
	    	starts = CollectionUtilities.observableList(tmp);
			TableUtilities.setViewToModel("LangebaanModel.xml", "UitslagEntry", getTable(), starts);
			TableColumn col2 = getTable().getColumnModel().getColumn(0);
			col2.setPreferredWidth(30);
			TableColumn col3 = getTable().getColumnModel().getColumn(1);
			col3.setPreferredWidth(70);
			TableColumn col4 = getTable().getColumnModel().getColumn(2);
			col4.setPreferredWidth(70);
			TableColumn col5 = getTable().getColumnModel().getColumn(3);
			col5.setPreferredWidth(30);
			TableColumn col6 = getTable().getColumnModel().getColumn(4);
			col6.setPreferredWidth(150);
		}
	}
	/*=====================================================================*/
	/**
	 * This method checks and formats the entered time.
	 * the time is entered as a string of digits representing the time in hundreths of seconds.
	 * 
	 * @param	tijd	Time to check and format
	 * @return			Formatted time
	 * @throws	TijdException
	 */
	private String CheckTijd(String tijd ) throws TijdException {
		String nwTijd = tijd;
		
		if( tijd.length()== 0 ) return nwTijd;
		
		if( Helper.isNumber(tijd) ) {
			if( tijd.length() > 7 ) {
				throw new TijdException("Tijd niet juist ingevoerd: teveel cijfers");
			}
			if( tijd.length() < 5 ){
				throw new TijdException("Tijd niet juist ingevoerd: niet voldoende cijfers");
			}
			if( tijd.length() == 5 ) {
				nwTijd = "00:0" + tijd.substring(0,1)+":"+tijd.substring(1,3)+"."+tijd.substring(3,5);
			}
			if( tijd.length() == 6 ) {
				nwTijd = "00:"+tijd.substring(0,2)+":"+tijd.substring(2,4)+"."+tijd.substring(4,6);
			}
			if( tijd.length() == 7 ) {
				nwTijd = "0"+tijd.substring(0,1)+":"+tijd.substring(1,3)+":"+tijd.substring(3,5)+"."+tijd.substring(5,7);
			}
		} else {
			if(		tijd.equalsIgnoreCase("agmk") ||
					tijd.equalsIgnoreCase("agzk") ||
					tijd.equalsIgnoreCase("na") ) {
				nwTijd = tijd.toLowerCase();
			} else {
				throw new TijdException("5 of 6 cijfers of agmk(=ziek),agzk(=niet gestart) of na(=niet aangekomen) is toegestaan.");
			}
		}
		return nwTijd;
	}

	/**
	 * This method initializes SplashImportButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getSplashImportButton() {
		if (SplashImportButton == null) {
			SplashImportButton = new JButton();
			SplashImportButton.setText("Splash Import");
			SplashImportButton.setEnabled(false);
			SplashImportButton.addActionListener(new SplashImportButtonActionListener());
		}
		return SplashImportButton;
	}
	

	/**
	 * This method initializes SelectDialog	
	 * 	
	 * @return javax.swing.JDialog	
	 */
	private JDialog getSelectDialog() {
		if (selectDialog == null) {
			selectDialog = new JDialog();
			selectDialog.setTitle("Keuze");
			selectDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			selectDialog.setModal(true);
			selectDialog.setContentPane(getSelectDialogContentPane());
			selectDialog.setSize(new Dimension(160, 200));
			selectDialog.setLocationRelativeTo(null);
		}
		return selectDialog;
	}

	/**
	 * This method initializes DialogContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getSelectDialogContentPane() {
		if (selectDialogContentPane == null) {
			selectDialogContentPane = new JPanel();
			selectDialogContentPane.setLayout(new BorderLayout());
			selectDialogContentPane.add(getSelectDialogScrollPane(), BorderLayout.CENTER);
			selectDialogContentPane.add(getSelectionText(), BorderLayout.NORTH);
			selectDialogContentPane.add(getSelectButton(), BorderLayout.SOUTH);
			selectDialogContentPane.setPreferredSize(new Dimension(160,200));
		}
		return selectDialogContentPane;
	}

	/**
	 * This method initializes SelectDialogScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getSelectDialogScrollPane() {
		if (selectDialogScrollPane == null) {
			selectDialogScrollPane = new JScrollPane();
			selectDialogScrollPane.setViewportView(getCapsList());
		}
		return selectDialogScrollPane;
	}

	/**
	 * This method initializes selectButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getSelectButton() {
		if (selectButton == null) {
			selectButton = new JButton();
			selectButton.setText("Kies");
			selectButton.setSize(new Dimension(40, 25));
			selectButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					getSelectDialog().setVisible(false);
					int sel = getCapsList().getSelectedIndex();
					start = mstarts.get(sel);
				}
				
			});
			
		}
		return selectButton;
	}

	/**
	 * This method initializes capsList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getCapsList() {
		if (capsList == null) {
			capsList = new JList();
			capsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			capsList.setSelectedIndex(0);
			capsList.setVisibleRowCount(-1);
		}
		return capsList;
	}
	
	/**
	 * This method initializes capsList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JTextArea getSelectionText() {
		if (selectionText == null) {
			selectionText = new JTextArea();
			selectionText.setText("Er zijn meerdere\nzwemmers met cap xx");
			selectionText.setEditable(false);
			selectionText.setEnabled(true);
			selectionText.setBackground(SystemColor.control);
			selectionText.setFont(new Font("Verdana", Font.PLAIN, 11));
			selectionText.setLineWrap(false);
		}
		return selectionText;
	}

	/**
	 * This method initializes extraButtonPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getExtraButtonPanel() {
		if (extraButtonPanel == null) {
			extraButtonPanel = new JPanel();
			extraButtonPanel.setLayout(new GridBagLayout());
			extraButtonPanel.add(getSplashImportButton());
		}
		return extraButtonPanel;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
